#include "GPSMeterPanel.h"

CGPSMeterPanel::CGPSMeterPanel(void)
{
    nmea_zero_INFO(&m_NMEAInfo);
}

CGPSMeterPanel::~CGPSMeterPanel(void)
{
}

void CGPSMeterPanel::SetNMEAInfo(const nmeaINFO info)
{
    m_NMEAInfo = info;
}

void CGPSMeterPanel::PaintInMemory()
{
    RECT *rect = &GetRect();
    HDC hMemoryDC = m_MemoryDC.GetDC();
    if (!hMemoryDC)
    {
        m_MemoryDC.Create(rect->right - rect->left, rect->bottom - rect->top);
        hMemoryDC = m_MemoryDC.GetDC();
    }

    HBRUSH hBrush;
    HPEN hPen;

    //backgroud
    hPen = CreatePen(PS_NULL, 1, RGB(0,0,0));
    hBrush = CreateSolidBrush(RGB(255,255,255));
    SelectObject(hMemoryDC, hPen);
    SelectObject(hMemoryDC, hBrush);
    Rectangle(hMemoryDC, 0, 0, rect->right - rect->left, rect->bottom - rect->top);
    DeleteObject(hPen);
    DeleteObject(hBrush);

    //meter panel bg
    hPen = CreatePen(PS_SOLID, 1, RGB(0,0,0));
    hBrush = CreateSolidBrush(RGB(255,255,255));
    SelectObject(hMemoryDC, hPen);
    SelectObject(hMemoryDC, hBrush);
    Ellipse(hMemoryDC, 0, 0, 300, 300);
    POINT point;
    MoveToEx(hMemoryDC, 0, 150, &point);
    LineTo(hMemoryDC, 300, 150);
    MoveToEx(hMemoryDC, 150, 0, &point);
    LineTo(hMemoryDC, 150, 300);
    DeleteObject(hBrush);
    DeleteObject(hPen);

    //direction arrow
    //m_NMEAInfo.direction = 315;
    hPen = CreatePen(PS_SOLID, 5, RGB(0,0,0));
    hBrush = CreateSolidBrush(RGB(0,0,0));
    SelectObject(hMemoryDC, hPen);
    SelectObject(hMemoryDC, hBrush);
    double dirRadian = m_NMEAInfo.direction/180 * NMEA_PI;
    POINT dirPoint;
    dirPoint.x = (int)(150+150*sin(dirRadian)+0.5);
    dirPoint.y = (int)(150-150*cos(dirRadian)+0.5);
    Ellipse(hMemoryDC, dirPoint.x-10, dirPoint.y-10, dirPoint.x+10, dirPoint.y+10);
    //MoveToEx(hMemoryDC, 150 - 150*sin(dirRadian), 150 + 150*cos(dirRadian), &point);
    //LineTo(hMemoryDC, 150 - 150*sin(dirRadian+NMEA_PI), 150 + 150*cos(dirRadian+NMEA_PI));
    DeleteObject(hBrush);
    DeleteObject(hPen);
}

void CGPSMeterPanel::PaintWin(HDC hdc, RECT* prcWin, RECT* prcUpdate)
{
    PaintInMemory();

    //copy memory to screen
    BitBlt(hdc,prcUpdate->left,prcUpdate->top,prcUpdate->right-prcUpdate->left,prcUpdate->bottom-prcUpdate->top,m_MemoryDC.GetDC(),0,0,SRCCOPY);

    return UiWin::PaintWin(hdc, prcWin, prcUpdate);
}
